package com.ikangtai.shecare.common.baseView.pickerDialog.recordPickerDialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import com.ikangtai.shecare.R;

import java.util.Calendar;

/**
 * Created by Administrator on 2016/1/23.
 */
public class DateTimePickerDialog extends AlertDialog implements
        OnClickListener {
    private DateTimePicker mDateTimePicker;
    private Calendar mDate = Calendar.getInstance();
    private OnDateTimeSetListener mOnDateTimeSetListener;
    @SuppressWarnings("deprecation")
    public DateTimePickerDialog(Context context, long date) {
        super(context);
        mDateTimePicker = new DateTimePicker(context,date);
        setView(mDateTimePicker);
        /*
         *实现接口，实现里面的方法
         */
        mDateTimePicker
                .setOnDateTimeChangedListener(new DateTimePicker.OnDateTimeChangedListener() {
                    @Override
                    public void onDateTimeChanged(DateTimePicker view, int hour, int minute) {
                        mDate.set(Calendar.HOUR_OF_DAY, hour);
                        mDate.set(Calendar.MINUTE, minute);
                        mDate.set(Calendar.SECOND, 0);
                    }
                });

        setButton(context.getString(R.string.setting), this);
        setButton2(context.getString(R.string.cancel), (OnClickListener) null);
        mDate.setTimeInMillis(date);
        setTitle(context.getString(R.string.time_setting));
    }

    /*
     *接口回調
     *控件 秒数
     */
    public interface OnDateTimeSetListener {
        void OnDateTimeSet(AlertDialog dialog, String time);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
        mOnDateTimeSetListener = callBack;
    }

    public void onClick(DialogInterface arg0, int arg1) {
        if (mOnDateTimeSetListener != null) {
            mOnDateTimeSetListener.OnDateTimeSet(this,getSelectTime(mDate.get(Calendar.HOUR_OF_DAY),mDate.get(Calendar.MINUTE)));
        }
    }

    private String getSelectTime(int hour, int minute){

        String hourStr = String.valueOf(hour);
        if (hour < 10) {
            hourStr = "0" + hourStr;
        }
        String minuteStr = String.valueOf(minute);
        if (minute < 10) {
            minuteStr = "0" + minuteStr;
        }
        return hourStr + ":" + minuteStr;
    }
}
